
var models = require('../models');
var questionModel = models.questionModel;
var idLen = 30;
Array.prototype.binaryFind = function(elem,start,end){
	//调用find之前需要先将元素按升序排序
	if(start == null){start = 0;}
	if(end == null){end = this.length-1;}
	if(start>end){return -1;}
	var mid = Math.floor((start+end)/2);
	if(this[mid] == elem){return mid;}
	var inLeft = this.find(elem,start,mid-1);
	if(inLeft>=0){return inLeft;}
	var inRight = this.find(elem,mid+1,end);
	if(inRight>=0){return inRight;}
	return -1;
}
var myTime = function(){
  var curTime = new Date();
  var year = curTime.getFullYear();
  var month = curTime.getMonth()+1;
  if(month<10){
    month = '0'+month;
  }
  var date = curTime.getDate();
  if(date<10){
    date = '0'+date;
  }
  var hour = curTime.getHours();
  if(hour<10){
    hour = '0'+hour;
  }
  var minute = curTime.getMinutes();
  if(minute<10){
    minute = '0'+minute;
  }
  var second = curTime.getSeconds();
  if(second<10){
    second = '0'+second;
  }
  var result = year+'-'+month+'-'+date+' '+hour+':'+minute+':'+second;
  return result;
}
exports.index = function(req,res){
	questionModel.find(function(err,data){
		if(err){
			throw(err);
		}
		else{
			res.render('index',{json:JSON.stringify(data)});
		}
	})
}
exports.createQuestion = function(req,res){
	var author = req.body.author;
	if(author == null||author.length != idLen){
		res.send({
			status:'error',
			message:'请指定author'
		});
		return;
	}
	var title = req.body.title;
	if(title == null){
		res.send({
			status:'error',
			message:'请指定title'
		});
		return;
	}
	var choices = req.body.choices;
	if(choices == null||choices.length<2){
		res.send({
			status:'error',
			message:'请指定至少两个选项'
		});
		return;
	}
	var newChoices = [];
	for(var i = 0; i < choices.length; i++){
		newChoices.push({
			text:choices[i],
			members:[]
		});
	}
	var now = myTime();
	var newQuestion = new questionModel({
		author:author,
		title:title,
		time:now,
		_time:Math.floor(new Date().getTime()),
		choices:newChoices,
		supporter:[],
		protester:[]
	});
	newQuestion.save(function(err){
		if(err){
			res.send({
				status:'error',
				message:'系统未知错误，错误码1000'
			})
		}
		else{
			res.send({
				status:'success'
			})
		}
	});
}
exports.submitQuestion = function(req,res){
	var memberId = req.body.memberId;
	if(memberId == null||memberId.length != idLen){
		res.send({
			status:'error',
			message:'请指定memberId'
		});
		return;
	}
	var questionId = req.body.questionId;
	if(questionId == null){
		res.send({
			status:'error',
			message:'请指定questionId'
		});
		return;
	}
	var choiceIndex = req.body.choiceIndex;
	if(choiceIndex == null||choiceIndex<0){
		res.send({
			status:'error',
			message:'请指定合法的choiceIndex'
		});
		return;
	}
	questionModel.findOne({'_id':questionId},function(err,question){
		if(err){
			res.send({
				status:'error',
				message:'系统未知错误，错误码1001'
			});
		}
		else{
			if(question){
				var len = question.choices.length;
				if(choiceIndex>=len){
					res.send({
						status:'error',
						message:'请指定合法的choiceIndex'
					});
					return;
				}
				question.choices[choiceIndex].members.push(memberId);
				question.save(function(err){
					if(err){
						res.send({
							status:'error',
							message:'系统未知错误，错误码1002'
						});
					}
					else{
						res.send({
							status:'success'
						});
					}
				})
			}
			else{
				res.send({
					status:'error',
					message:'请指定合法的questionId'
				});
			}
		}
	})
}
exports.supportQuestion = function(req,res){
	var memberId = req.body.memberId;
	if(memberId == null||memberId.length != idLen){
		res.send({
			status:'error',
			message:'请指定memberId'
		});
		return;
	}
	var questionId = req.body.questionId;
	if(questionId == null){
		res.send({
			status:'error',
			message:'请指定questionId'
		});
		return;
	}
	questionModel.findOne({'_id':questionId},function(err,question){
		if(err){
			res.send({
				status:'error',
				message:'系统未知错误，错误码1003'
			});
		}
		else{
			if(question){
				var index = question.supporter.find(memberId);
				if(index>=0){
					res.send({
						status:'error',
						message:'你已经点过赞了'
					});
					return;
				}
				question.supporter.push(memberId);
				question.save(function(err){
					if(err){
						res.send({
							status:'error',
							message:'系统未知错误，错误码1004'
						});
					}
					else{
						res.send({
							status:'success'
						});
					}
				})
			}
			else{
				res.send({
					status:'error',
					message:'请指定合法的questionId'
				});
			}
		}
	})
}
exports.opposeQuestion = function(req,res){
	var memberId = req.body.memberId;
	if(memberId == null||memberId.length != idLen){
		res.send({
			status:'error',
			message:'请指定memberId'
		});
		return;
	}
	var questionId = req.body.questionId;
	if(questionId == null){
		res.send({
			status:'error',
			message:'请指定questionId'
		});
		return;
	}
	questionModel.findOne({'_id':questionId},function(err,question){
		if(err){
			res.send({
				status:'error',
				message:'系统未知错误，错误码1003'
			});
		}
		else{
			if(question){
				var index = question.protester.find(memberId);
				if(index>=0){
					res.send({
						status:'error',
						message:'你已经点过反对了'
					});
					return;
				}
				question.protester.push(memberId);
				question.save(function(err){
					if(err){
						res.send({
							status:'error',
							message:'系统未知错误，错误码1005'
						});
					}
					else{
						res.send({
							status:'success'
						});
					}
				})
			}
			else{
				res.send({
					status:'error',
					message:'请指定合法的questionId'
				});
			}
		}
	})
}